﻿using GDGeek;
using MrPP.iot.Guide;
using MrPP.UX;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static MrPP.SerriedCabinet.CabinetGroup;

namespace MrPP.SerriedCabinet
{
    public class BoxManager : MonoBehaviour
    {

        [SerializeField]

        private Transform _boxs;

        [SerializeField]
        private Data _data;
        [SerializeField]
        private Transform _offset;

        [SerializeField]
        private GuidePlane _plane;
        [SerializeField]
        private Box _phototype;
        [System.Serializable]
        public class Data
        {
            public Box.Data[] item;

            public float height;
            public float deep;
            public float gap;
            public GDGeek.Vector2Rounding offset;
            public int fix;
        }
       
        internal Cell getCell(BoxLocation location)
        {
            if (location.box < 0 || location.box >= list.Count) {
                return null;
            }
            Box box = this.list[location.box];

            return box.getCell(location);
        }

        [SerializeField]
        private List<Box> _list = new List<Box>();
        public List<Box> list {
            get {
                return _list;
            }
        }
        public Box getBox(int index) {

            return _list[index];
        }
        void Start() {

            if (CabinetGroup.IsInitialized)
            {
                CabinetGroup.Instance.onChange += onChange;
            }
            if (CellManager.IsInitialized) {
                CellManager.Instance.onState += doState;
            }
        }

        private void doState(CellManager.Type type)
        {
            if (type == CellManager.Type.Shelve) {
                foreach (var box in _list) {
                    box.cube.GetComponent<BoxCollider>().enabled = false;
                }
            }
            else
            {
                foreach (var box in _list)
                {
                    box.cube.GetComponent<BoxCollider>().enabled = true;
                }
            }
        }

        void OnDestroy() {
            if (CabinetGroup.IsInitialized) {
               CabinetGroup.Instance.onChange -= onChange;
            }
        }
        public void onChange() {

            if (CabinetGroup.IsInitialized)
            {
                for (int i = 0; i < CabinetGroup.Instance.count; ++i)
                {
                    var item = CabinetGroup.Instance.getItem(i);
                    float posi = item.getPosition();
                    if (_list.Count <= i) {
                        Debug.LogError("is error" + _list.Count);
                    }
//                    Debug.LogError(i + "box" + item.position);
                    _list[i].setPosition(posi);
                }
            }
        }
        public void setup(Data data) {
            this._data = data;
            refresh();
            onChange();
        }
    
        public void refresh() {
            foreach (var box in _list) {
                DestroyImmediate(box.gameObject);
            }
            _list.Clear();
            float x = 0;
            _plane.transform.SetParent(this.transform, false);
            _plane.transform.localEulerAngles = new Vector3(0,0,0);
            _plane.transform.localPosition = new Vector3(0, -_data.height, 0);
            for (int i = 0; i < _data.item.Length; ++i) {
                var data = _data.item[i];

                var box = GameObject.Instantiate(_phototype);
                box.transform.SetParent(this._boxs, false);
                box.setup(i, data, _data.height, _data.deep);
                x += data.width / 2f;
                box.transform.localPosition = new Vector3(x, 0, 0);
                box.gameObject.SetActive(true);
                x += data.width / 2f;
                IntId id = box.gameObject.AskComponent<IntId>();
                id.id = i;
                _list.Add(box);
            }
           
            var offset = this._data.offset.toVector2();
           
            float width = 0f;
            for (int i = 0; i < _data.item.Length; ++i) {
                if (i == _data.fix) {
                    width += _data.item[i].width / 2;
                    break;
                }
                width += _data.item[i].width;
            }

            this.transform.localPosition = new Vector3(-width - offset.x, _data.height/2 - offset.y, _data.deep / 2f);
            Debug.Log(this.transform.localPosition);

        }

        public void lowlight()
        {
            foreach (var box in this._list) {
                box.lowlight();
            }
        }

        public void highlight(CabinetGroup.ItemData data)
        {
           // Debug.LogError("light:" + data.text + data.box);
            for (int i = 0; i < this._list.Count; ++i) {
                if (i == data.box)
                {
                    this._list[i].highlight(data);
                }
                else {
                    this._list[i].lowlight();
                }
            }
          
           


        }
    }
}